package com.sevenstudio.adu.gasing.state;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ScrollDetector;
import org.andengine.util.color.Color;

import android.view.KeyEvent;

import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.element.AAnimatedSprite;
import com.sevenstudio.adu.gasing.element.ABackground;
import com.sevenstudio.adu.gasing.element.AHud;
import com.sevenstudio.adu.gasing.element.AScene;
import com.sevenstudio.adu.gasing.element.ASprite;
import com.sevenstudio.adu.gasing.element.AStrokeText;
import com.sevenstudio.adu.gasing.element.AText;
import com.sevenstudio.adu.gasing.engine.Anchor;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Utils;



public class State_Credits 
{
	GameEngine engine;
	AScene scene;
	HUD hud;
	
	ABackground bg_credits;
	ASprite logo_second, logo_first;
	AAnimatedSprite spr_gasing;
		
	private int JUMLAH_NAMA = 5;
	private int JUMLAH_JUDUL = 4;
	
	AText[] txt_Judul = new AText[JUMLAH_JUDUL];
	AText[] txt_Nama = new AText[JUMLAH_NAMA];
	
	private float y;
	
	private boolean stop;
	
	public State_Credits(GameEngine engine)
	{
		this.engine = engine;
		scene = new AScene(0);
		hud = new HUD();
		
		Load();
	}

	private void Load() 
	{
		bg_credits = new ABackground(engine, iGameDefines.SPRITE_CREDITS, 0);
		logo_second = new ASprite(engine, iGameDefines.SPRITE_CREDITS, 1);
//		logo.SetPos(getCenterX(logo), logo.GetY());
		logo_first = new ASprite(engine, iGameDefines.SPRITE_CREDITS, 3);
		
		spr_gasing = new AAnimatedSprite(engine, iGameDefines.ANIM_MENU, 0);
		spr_gasing.theSprite().animate(50);
		
		for(int i =0; i < JUMLAH_JUDUL;i++)
		{
//			txt_Judul[i] = new AStrokeText(engine, iGameDefines.FONT_STROKE, Utils.GetRatioHeight(30), Color.GREEN, 0.1f, Color.BLACK, iGameDefines.TXT_CREDITS[i]);
			txt_Judul[i] = new AText(engine, iGameDefines.TEXT_STRING_CREDITS, 0+i, engine.gameFont[1]);
			txt_Judul[i].theText().setColor(Color.YELLOW);
//			txt_Judul[i].setPos(Utils.GetAnchorX(txt_Judul[i].theText().getWidth(), Anchor.CENTER), txt_Judul[i].theText().getY());
		}
		
		for(int i =0; i < JUMLAH_NAMA;i++)
		{
//			txt_Nama[i] = new AStrokeText(engine, iGameDefines.FONT_STROKE, Utils.GetRatioHeight(30), Color.BLACK, 0.1f, Color.YELLOW, iGameDefines.TXT_CREDITS_1[i]);
					
			txt_Nama[i]	 = new AText(engine, iGameDefines.TEXT_STRING_CREDITS, 5+i, engine.gameFont[3]);
//			txt_Nama[i].setPos(Utils.GetAnchorX(txt_Nama[i].theText().getWidth(), Anchor.CENTER), txt_Nama[i].theText().getY());
		}
		
		
		
		scene.theScene().setOnAreaTouchListener(engine);
		scene.theScene().setOnSceneTouchListener(engine);
		
	}

	public void Init() 
	{
		setPosAwal();
		engine.SetGameScene(scene);
//		engine.HudAttach(hud, logo);
	
		engine.SceneAttach(scene, bg_credits);
		engine.SceneAttach(scene, logo_second);
		engine.SceneAttach(scene, logo_first);
		engine.SceneAttach(scene, spr_gasing);
		Utils.TRACE("logo X "+logo_second.GetX());
		
//		hud.attachChild(logo.theSprite());
		
		for(int i =0; i < JUMLAH_JUDUL;i++)
		{
			engine.SceneAttach(scene, txt_Judul[i]);
//			hud.attachChild(txt_Judul[i].theText());
		}
		
		for(int i =0; i < JUMLAH_NAMA;i++)
		{
			engine.SceneAttach(scene, txt_Nama[i]);
//			hud.attachChild(txt_Nama[i].theText());
		}
		
		engine.SetNextMessageState(iGameDefines.MESSAGE_STATE_UPDATE);
	}

	public void Update()
	{
		if(!stop)
		{	
			y=-0.5f;
			
			logo_second.SetPos(logo_second.GetX(), logo_second.GetY() + y);
			for(int i =0; i < JUMLAH_JUDUL;i++)
			{
				txt_Judul[i].theText().setPosition(txt_Judul[i].theText().getX(), txt_Judul[i].theText().getY() + y);
			}
			
			for(int i =0; i < JUMLAH_NAMA;i++)
			{
				txt_Nama[i].theText().setPosition(txt_Nama[i].theText().getX(), txt_Nama[i].theText().getY() + y);
			}
			
			if(logo_second.GetY() + logo_second.theSprite().getHeight() < 0)
			{
				setPosAwal();
			}
			
			logo_first.SetPos(logo_first.GetX(), logo_first.GetY() + y);
		}
		else
		{
			logo_second.SetPos(logo_second.GetX(), logo_second.GetY());
			for(int i =0; i < JUMLAH_JUDUL;i++)
			{
				txt_Judul[i].theText().setPosition(txt_Judul[i].theText().getX(), txt_Judul[i].theText().getY());
			}
			
			for(int i =0; i < JUMLAH_NAMA;i++)
			{
				txt_Nama[i].theText().setPosition(txt_Nama[i].theText().getX(), txt_Nama[i].theText().getY());
			}
			
			logo_first.SetPos(logo_first.GetX(), logo_first.GetY());
		}
	}

	public void Exit() 
	{
		final EngineLock lock = engine.getEngine().getEngineLock();
		lock.lock();
		
		bg_credits.DetachSelf();
		logo_second.DetachSelf();
		logo_first.DetachSelf();
		
		spr_gasing.theSprite().detachSelf();
		
		for(int i =0; i < JUMLAH_JUDUL;i++)
		{
			txt_Judul[i].theText().detachSelf();
		}
		
		for(int i =0; i < JUMLAH_NAMA;i++)
		{
			txt_Nama[i].theText().detachSelf();
		}
		
		lock.unlock();
	}
	
	float getCenterX(ASprite aSprite)
	{
		return ((State_Main_Activity.CAMERA_WIDTH - aSprite.theSprite().getWidth()) /2); 
	}

	float getCenterX(AText txt)
	{
		return ((State_Main_Activity.CAMERA_WIDTH - txt.theText().getWidth()) /2); 
	}
	
	public void keyDown(int keyCode, KeyEvent event)
	{
	
		Exit();
		engine.SetNextMessageState(0);
		engine.SetNextGameState(iGameDefines.GAME_STATE_MAINMENU);
	}
	
	void setPosAwal()
	{
		float MAX_Y = State_Main_Activity.CAMERA_HEIGHT + Utils.GetRatioHeight(100);
		float MAX_Y_LOGO_SEVEN = State_Main_Activity.CAMERA_HEIGHT;

		float PADDING_JUDUL = Utils.GetRatioHeight(20);
		float PADDING = Utils.GetRatioHeight(5);
		float Y_LOGO =  MAX_Y + Utils.GetRatioHeight(600) + PADDING_JUDUL;		
		
		float Y_JUDUL[] = 
			{
				MAX_Y + Utils.GetRatioHeight(110) + PADDING_JUDUL,
				MAX_Y + Utils.GetRatioHeight(210) + PADDING_JUDUL,
				MAX_Y + Utils.GetRatioHeight(360) + PADDING_JUDUL,
				MAX_Y + Utils.GetRatioHeight(460) + PADDING_JUDUL,
				MAX_Y + Utils.GetRatioHeight(560) + PADDING_JUDUL,
			};
		
		float Y_NAMA[] = 
			{
				MAX_Y + Utils.GetRatioHeight(130) + PADDING + PADDING_JUDUL,
				MAX_Y + Utils.GetRatioHeight(230) + PADDING + PADDING_JUDUL,
				MAX_Y + Utils.GetRatioHeight(290) + PADDING,
				MAX_Y + Utils.GetRatioHeight(380) + PADDING + PADDING_JUDUL,
				MAX_Y + Utils.GetRatioHeight(480) + PADDING + PADDING_JUDUL,
				
			};
		
		
		logo_first.SetPos(Utils.GetAnchorX(logo_first.theSprite().getWidth(), Anchor.CENTER), MAX_Y_LOGO_SEVEN);
		for(int i =0; i < JUMLAH_JUDUL;i++)
		{
			txt_Judul[i].theText().setPosition(txt_Judul[i].theText().getX(), Y_JUDUL[i]);
		}
		
		for(int i =0; i < JUMLAH_NAMA;i++)
		{
			txt_Nama[i].theText().setPosition(txt_Nama[i].theText().getX(), Y_NAMA[i]);
		}
		
		logo_second.SetPos(Utils.GetAnchorX(logo_second.theSprite().getWidth(), Anchor.CENTER), Y_LOGO);
	}

	public void SceneTaouch(Scene arg0, TouchEvent arg1) {
	
		if(arg1.isActionDown())
		{
			stop = true;
		}
		
		if(arg1.isActionUp())
		{
			stop = false;
		}
		
		if(arg1.isActionMove())
		{
			
		}
	}

	public void onScroll(ScrollDetector pScollDetector, int pPointerID,
			float pDistanceX, float pDistanceY) 
	{
		
		logo_second.SetPos(logo_second.GetX(), logo_second.GetY() + pDistanceY);
		for(int i =0; i < JUMLAH_JUDUL;i++)
		{
			txt_Judul[i].theText().setPosition(txt_Judul[i].theText().getX(), txt_Judul[i].theText().getY() + pDistanceY);
		}
		
		for(int i =0; i < JUMLAH_NAMA;i++)
		{
			txt_Nama[i].theText().setPosition(txt_Nama[i].theText().getX(), txt_Nama[i].theText().getY() + pDistanceY);
		}
	
		logo_first.SetPos(logo_first.GetX(), logo_first.GetY() + pDistanceY);

		
	}
	
	
}
